<?php
namespace Common\Model;

class SiteTypeModel extends Model {
    protected $tableName = 'site_type';
    protected $pk        = 's_t_id';

    /**
     * buildSearchExpr 
     * 组装查询条件
     * 
     * @param mixed $d  查询数据，一般来源于$_GET或$_POST
     * @param mixed $field  可查询的字段对照表，其结构为array(array('表单项目名称' => '数据库字段名称'))
     * @access public
     * @return void 返回值为数组，参见方法内部注释
     */
    public function buildSearchExpr($d, $field = NULL) {
        $expr = array();
        $dao  = array();
        $sortExpr = 's_t_id DESC';

        /**
         * 下标为表单中的项目名称，值为数据库中表的字段名称 
         */
        if(empty($field)) {
            $field = array(
                's_t_id'      => 's_t_id',
            );
        }

        foreach($field as $k => $v) {
            $val = isset($d[$k]) ? trim($d[$k]) : '';

            if(strlen($val) < 1) {
                continue;
            }

            switch($k) {

                default:
                    $expr[$v] = $d[$k];
            }

            $dao[$k] = $d[$k];
        }

        if(!empty($d['sort']) && in_array($d['sort'], $this->getDbFields())) {
            $sortdir = empty($d['sortdir'])                     ? 'DESC'   : strtoupper($d['sortdir']);
            $sortdir = in_array($sortdir, array('DESC', 'ASC')) ? $sortdir : 'DESC';

            $sort        = $d['sort'];
            $dao['sort'] = $sort;
            $sortExpr    = "{$sort} {$sortdir}";

            $dao['sortdir'] = $sortdir;
        }

        return array(
            'expr' => $expr,            //ThinkPHP查询条件表达式
            'sort' => $sortExpr,        //排序表达式
            'json' => json_encode($dao) //用于回填至查询表单的数据
        );
    }

    public function save2($d) {
        $action = empty($d['s_t_id']) ? 'add' : 'save';

        if(empty($d['s_t_name'])) {
            throw new ThinkException('请输入名称');
        }

        $d['s_t_pid'] = empty($d['s_t_pid']) ? 0 : (int)$d['s_t_pid'];

        if(!$this->getBySTPid($d['s_t_pid'])) {
            throw new ThinkException('指定的分类不存在');
        }

        $id = empty($d['s_t_id']) ? null : $d['s_t_id'];

        if($this->checkName($d['s_t_name'], $d['s_t_pid'], $id)) {
            throw new ThinkException('名称已经存在，请换用其它名称');
        }

        return $this->$action($d);
    }

    public function checkName($name, $pid = 0, $id = null) {
        $expr = array(
            's_t_name' => $name,
            's_t_pid'  => $pid,
        );

        if(!empty($id)) {
            $expr['s_t_id'] = array('EXP', "NEQ {$id}");
        }

        return $this->where($expr)->find();
    }

    /**
     * del
     *
     * @param mixed $id
     * @return void
     */
    public function del($id) {
        $id   = (int)$id;

        if(!$record = $this->getBySTId($id)) {
            return true;
        }

        $this->startTrans();

        if(empty($record['s_t_pid'])) {
            $expr = array('s_t_pid' => $record['s_t_id']);

            $this->where($expr)->delete();
        }

        $expr = array( 's_t_id' => $id);
        $this->where($expr)->delete();
        $this->commit();

        return true;
    }

    public function getByPid($pid = 0) {
        $pid  = (int)$pid;
        $expr = array('s_t_pid' => $pid);

        return ($r = $this->field('s_t_id AS id, s_t_pid AS pid, s_t_name AS name')->where($expr)->select()) ? $r : false;
    }

}
